package com.charon.module.music.mercuty.service;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.charon.common.config.Global;
import com.charon.common.util.ConnectionHandler;
import com.charon.common.util.DateUtils;
import com.charon.common.util.wangyi.WYHandler;
import com.charon.common.util.wangyi.entity.WYSongListEntity;
import com.charon.common.util.wangyi.entity.WYUser;
import com.charon.module.music.dao.SongCollectEntityMapper;
import com.charon.module.music.dao.SongLikeEntityMapper;
import com.charon.module.music.dao.SongSickEntityMapper;
import com.charon.module.music.entity.SongCollectEntity;
import com.charon.module.music.entity.SongLikeEntity;
import com.charon.module.music.entity.SongSickEntity;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Service
public class MercutyMusicService {
	//本机服务器地址
	private static String baseUrl = Global.getConfig("wangyi.url");
	protected static Logger logger = LoggerFactory.getLogger(ConnectionHandler.class);
	@Autowired
	SongCollectEntityMapper scem;
	
	@Autowired
	SongSickEntityMapper ssem;
	
	@Autowired
	SongLikeEntityMapper slem;
	
	public String songCollect(Integer songID,Integer type){
		SongCollectEntity entity = new SongCollectEntity();
		entity.setCreateBy("mercury");
		entity.setSongId(songID);
		entity.setUserId(2);
		List<SongCollectEntity> list = scem.selectByEntity(entity);
		entity.setSongPlatformAlbumId(null);
		if(type==1){
			entity.setSongPlatformId("网易");
		}else if(type==2){
			entity.setSongPlatformId("酷狗");
		}
		entity.setUpdateBy("mercury");
		entity.setDelFlag("1");
		entity.setCreateDate(DateUtils.formatDateTime(new Date()));
		entity.setUpdateDate(DateUtils.formatDateTime(new Date()));
		int status=0;
		if(list!=null&&list.size()>0){
			entity.setId(list.get(0).getId());
			if("0".equals(list.get(0).getCollectFlag())){
				entity.setCollectFlag(1+"");
			}else{
				entity.setCollectFlag(0+"");
			}
			status=scem.updateByPrimaryKey(entity);
		}else{
			entity.setCollectFlag(1+"");
			status=scem.insert(entity);
		}
		logger.info(entity.toString());
		if(status==0){
			return "暂无数据";
		}
		return "成功";
	}
	

	
	public String songSick(Integer songID,Integer type){
		SongSickEntity entity = new SongSickEntity();
		entity.setCreateBy("mercury");
		entity.setSongId(songID);
		entity.setUserId(2);
		List<SongSickEntity> list = ssem.selectByEntity(entity);
		entity.setSongPlatformAlbumId(null);
		if(type==1){
			entity.setSongPlatformId("网易");
		}else if(type==2){
			entity.setSongPlatformId("酷狗");
		}
		entity.setUpdateBy("mercury");
		entity.setDelFlag("1");
		entity.setCreateDate(DateUtils.formatDateTime(new Date()));
		entity.setUpdateDate(DateUtils.formatDateTime(new Date()));
		int status=0;
		if(list!=null&&list.size()>0){
			entity.setId(list.get(0).getId());
			if("0".equals(list.get(0).getSickFlag())){
				entity.setSickFlag(1+"");
			}else{
				entity.setSickFlag(0+"");
			}
			status=ssem.updateByPrimaryKey(entity);
		}else{
			entity.setSickFlag(1+"");
			status=ssem.insert(entity);
		}
		if(status==0){
			return "暂无数据";
		}
		return "成功";
	}
	
	
	public String songLike(Integer songID,Integer type){
		SongLikeEntity entity = new SongLikeEntity();
		entity.setCreateBy("mercury");
		entity.setSongId(songID);
		entity.setUserId(2);
		List<SongLikeEntity> list = slem.selectByEntity(entity);
		entity.setSongPlatformAlbumId(null);
		if(type==1){
			entity.setSongPlatformId("网易");
		}else if(type==2){
			entity.setSongPlatformId("酷狗");
		}
		entity.setUpdateBy("mercury");
		entity.setDelFlag("1");
		entity.setCreateDate(DateUtils.formatDateTime(new Date()));
		entity.setUpdateDate(DateUtils.formatDateTime(new Date()));
		int status=0;
		if(list!=null&&list.size()>0){
			entity.setId(list.get(0).getId());
			if("0".equals(list.get(0).getLikeFlag())){
				entity.setLikeFlag(1+"");
			}else{
				entity.setLikeFlag(0+"");
			}
			status=slem.updateByPrimaryKey(entity);
		}else{
			entity.setLikeFlag(1+"");
			status=slem.insert(entity);
		}
		if(status==0){
			return "暂无数据";
		}
		return "成功";
	}
	
	
	
	
	/**
	 * 获取播放地址（暂时不用）
	 * @param songID
	 * @return
	 */
	public String getSongURL(String songID){
		JSONObject obj = WYHandler.getSongURL(songID);
		if(obj==null||"".equals(obj.toString())){
			String songUrl="http://music.163.com/song/media/outer/url?id="+songID+".mp3";
		}
		return obj.toString();
	}
	
	/**
	 * 根据歌单ID获取歌曲列表
	 * @param listID
	 * @return
	 */
	public JSONObject getSongList(String listID) {
		return WYHandler.getSongList(listID);
	}
	
	/**
	 * 搜索
	 * @param keyWords
	 * @param limit
	 * @param offset
	 * @param type
	 * @return
	 */
	public JSONObject searchByType(String keyWords,String limit,String offset,String type){
		try {
			return WYHandler.searchByType(keyWords, limit, offset, type);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 获取用户歌单
	 * @param userID
	 * @return
	 */
	public List<WYSongListEntity> getUserSongLists(String userID){
		JSONObject jsonLists = WYHandler.getUserSongLists(userID);
		ArrayList<WYSongListEntity> list=new ArrayList<WYSongListEntity>();
		if(jsonLists!=null&&jsonLists.size()>0){
			JSONArray jsonArray = jsonLists.getJSONArray("playlist");
			for(int i=0;i<jsonArray.size();i++){
				JSONObject obj = jsonArray.getJSONObject(i);
				WYSongListEntity wle=new WYSongListEntity();
				wle.setName(obj.getString("name"));
				wle.setWYID(obj.getString("id"));
				wle.setCoverImgUrl(obj.getString("coverImgUrl"));
				wle.setCreateTime(obj.getString("createTime"));
				wle.setDescription(obj.getString("description"));
				wle.setPlayCount(obj.getString("playCount"));
				wle.setStatus(obj.getString("status"));
				wle.setSubscribedCount(obj.getString("subscribedCount"));
				StringBuilder tags=new StringBuilder();
				if(null!=obj.getJSONArray("tags")&&obj.getJSONArray("tags").size()>0){
					for(int a=0;a<obj.getJSONArray("tags").size();a++){
						if(a!=(obj.getJSONArray("tags").size()-1)){
							tags.append(obj.getJSONArray("tags").get(a)+",");
						}else{
							tags.append(obj.getJSONArray("tags").get(a));
						}
					}
				}
				wle.setTags(tags.toString());
				wle.setTrackCount(obj.getString("trackCount"));
				wle.setTrackNumberUpdateTime(obj.getString("trackNumberUpdateTime"));
				wle.setUpdateTime(obj.getString("updateTime"));
				JSONObject creator = obj.getJSONObject("creator");
				wle.setUserId(creator.getString("userId"));
				WYUser user=new WYUser();
				user.setAvatarUrl(creator.getString("avatarUrl"));
				user.setBackgroundUrl(creator.getString("backgroundUrl"));
				user.setBirthday(creator.getString("birthday"));
				user.setDescription(creator.getString("description"));
				user.setNickName(creator.getString("nickname"));
				user.setUserType(creator.getString("userType"));
				user.setSignature(creator.getString("signature"));
				user.setWYId(creator.getString("userId"));
				wle.setUser(user);
				list.add(wle);
			}
		}
		logger.info(list.toString());
		return list;
	}
	
	/**
	 * 获取歌词
	 * @param songID
	 * @return
	 */
	public JSONObject getLrc(String songID){
		JSONObject obj = WYHandler.getLrc(songID);
		if(obj==null||"".equals(obj.toString())){
			return null;
		}
		return obj;
	}
	
	/**
	 * @Description: 检查歌曲是否有效 
	 * @Author charon [248135724@qq.com]
	 * @Date:   2018年11月15日 下午3:18:27 
	 * @param songID 
	 * @return { success: true, message: 'ok' } 或者 { success: false, message: '亲爱的,暂无版权' }
	 */
	public String checkSong(String songID){
		JSONObject obj = WYHandler.checkSong(songID);
		if(obj==null||"".equals(obj.toString())){
			return "暂无数据";
		}
		return obj.toString();
	}
	
	/**
	 * @Description: 获取热门歌单 
	 * @Author charon [248135724@qq.com]
	 * @Date:   2018年11月15日 下午8:34:20 
	 * @return
	 */
	public JSONObject getHighQuality(String limit,String updateTime){
		JSONObject obj = WYHandler.getHighQuality(limit,updateTime);
		if(obj==null||"".equals(obj.toString())){
			return null;
		}
		return obj;
	}
	
	/**
	 * @Description: 获取网友精选歌单
	 * @Author charon [248135724@qq.com]
	 * @Date:   2018年11月15日 下午8:35:22 
	 * @return
	 */
	public String getNetFriendLists(String limit){
		JSONObject obj = WYHandler.getNetFriendLists(limit);
		if(obj==null||"".equals(obj.toString())){
			return "暂无数据";
		}
		return obj.toString();
	}
	
	
	public JSONObject getSongInfo(String songID){
		JSONObject obj = WYHandler.getSongInfo(songID);
		if(obj==null||"".equals(obj.toString())){
			return null;
		}
		return obj;
	}
}
